********************************************************************************
** 	TITLE:  c3_main_iv.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Table III: Effects of IGNITE on Misconduct and Recidivism

********************************************************************************
**  PURPOSE: IV estimate
********************************************************************************
		set sortseed 13
*******Complier control means for number of major misconducts and 3 months recidivism.
//Code taken from "$code/c_complier_control_mean.do"
use "$output_data/estimate.dta", clear
cap drop stay_day
cap drop less_than_3
gen stay_day = stay_rec*30
gen less_than_3 = stay_day<=3

	foreach y in return_3m {
		cap gen `y'_less_than_3 = `y'*less_than_3
		ivreghdfe  `y'_less_than_3 ( less_than_3 = i.D_remove_any) ///
			 if  `y'>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control)
		global ccm_`y' : di %9.3f _b[less_than_3]
	}

cap drop stay_day
cap drop less_than_3
gen stay_day = stay_misc*30
gen less_than_3 = stay_day<=3

	foreach y in num_maj_weekly {
		cap gen `y'_less_than_3 = `y'*less_than_3
		ivreghdfe  `y'_less_than_3 ( less_than_3 = i.D_remove_any) ///
			 if  `y'>=0&D_ignite_proxy, ///
			cluster(inmate) a($design_control)
		global ccm_`y' : di %9.3f _b[less_than_3]
	}
		
	
********************************************************************************
* Load Data
********************************************************************************
	use "$output_data/estimate.dta", replace
	
********************************************************************************
* Regressions
********************************************************************************
	
	est clear

	preserve
	
	local remove D_remove_any
	local interact interact_proxy_any
		foreach v in  num_maj_weekly return_3m {
			//stay ignite_0 for misconduct
			if "`v'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`v'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
			
			forvalues j=0/1{
				if `j'==0{
					local controls " "
				}
				if `j'==1{
					local controls $auxiliary_control
				}
				
			* IV				
				ivreghdfe `v' (ignite_0 stay = `interact' `remove') `controls' D_ignite_proxy ///
					if `v'>=0, ///
					cluster(inmate) ffirst a($design_control)
				eststo tsls`v'`j'any
				estadd scalar clust e(N_clust): tsls`v'`j'any
				mat SWF=e(first)
				local temp_f1 = SWF[8,1]
				local temp_f2 = SWF[8,2]
				local f1: display %8.3f `temp_f1'
				local f2: display %8.3f `temp_f2'
				estadd local f1 "`f1'" : tsls`v'`j'any
				estadd local f2 "`f2'" : tsls`v'`j'any
				
				estadd local meancont "${ccm_`v'}" : tsls`v'`j'any
				local fstat = string(e(rkf), "%8.3f")
				estadd local fstat_2 "`fstat'": tsls`v'`j'any
				
			* summ of ignite effect and jail effect
				lincom ignite_0+stay
				
				local lintemp = r(estimate)
				local lin: display %8.3f `lintemp'
				
				local linsetemp = r(se)
				local linse: display %8.3f `linsetemp'
				
				local lin_p  =r(p)
				
				
				if `lin_p'<0.01 {
					local star1="***"
				}
				if `lin_p'>=0.01 & `lin_p'<0.05 {
					local star1="**"
				}
				if `lin_p'>=0.05 & `lin_p'<0.1 {
					local star1="*"
				}
				if `lin_p'>=0.1 {
					local star1=""
				}
				
				local linc = "`lin'" + "`star1'"
				estadd local combined "`linc'": tsls`v'`j'any
				local lincse = subinstr("(`linse')"," ","",.)
				estadd local combined_se "`lincse'": tsls`v'`j'any
				
					if `j'==0{
						estadd local basecont "Yes": tsls`v'`j'any
						estadd local demo_case "No": tsls`v'`j'any
					}
					if `j'==1{
						estadd local basecont "Yes": tsls`v'`j'any`g'`i'
						estadd local demo_case "Yes": tsls`v'`j'any`g'`i'
					}		
				
				
			qui{
				
			* Reduced form
				reghdfe `v' `interact' `remove'  D_ignite_proxy `controls' ///
					if `v'>=0, ///
					cluster(inmate) a($design_control)
				eststo rf`v'`j'any
				sum `v' if e(sample)==1 /*& D_ignite_proxy==0*/ &`remove'==0
				local meantemp = r(mean)
				local mean: display %8.3f `meantemp' 			
				estadd local meancont"`mean'" : rf`v'`j'any
				local fstat = string(e(rkf), "%8.3f")
				estadd local fstat_2 "`fstat'": rf`v'`j'any
				}
			}
		}	

********************************************************************************
* Make Tables
********************************************************************************
			
			*****main IV
			estout tslsnum_maj_weekly0any tslsnum_maj_weekly1any   ///
				tslsreturn_3m0any tslsreturn_3m1any ///
				using "$tables/table3b_2sls.tex", style(tex)  ///
				label nobaselevels collabels(none) mlabels(none) /// 
				keep(stay ignite_0) ///
				order(ignite_0 stay) ///
				varlabel(ignite_0 "Months in IGNITE" stay "Months in Jail") ///
				cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				stats(foo combined combined_se foo1 meancont basecont demo_case N, ///
				fmt(0 %8.3f %8.3f 0 %8.3f %8.3f %8.3f 0 0 0 0 0 0 0 0 0 0 0 0) /// 
				labels("\hspace{0.5cm}" "Months in IGNITE+Months in Jail" "\hspace{0.5cm}" "\hspace{0.5cm}"  "Complier Control Mean" ///
					"Design Controls" "Auxiliary Controls" "Observations"))	replace
			
			*****main RF
			estout rfnum_maj_weekly0any   rfnum_maj_weekly1any ///
				rfreturn_3m0any   rfreturn_3m1any ///
				using "$tables/table3a_rf.tex", style(tex)  ///
				label nobaselevels collabels(none) mlabels(none) /// 
				keep(D_remove_any  interact_proxy_any) ///
				order(interact_proxy_any D_remove_any ) ///
				varlabel(interact_proxy_any "Court Delay $\times$ Post-IGNITE" D_remove_any "Court Delay") ///
				cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) /// 
				substitute(no {No} yes {Yes}) /// 
				replace

